package flaud

import (
	"context"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/flaud"
	flaudReq "github.com/flipped-aurora/gin-vue-admin/server/model/flaud/request"
)

type CrimeElementTypeService struct{}

// CreateCrimeElementType 创建罪量要素管理记录
// Author [yourname](https://github.com/yourname)
func (CETService *CrimeElementTypeService) CreateCrimeElementType(ctx context.Context, CET *flaud.CrimeElementType) (err error) {
	err = global.GVA_DB.Create(CET).Error
	return err
}

// DeleteCrimeElementType 删除罪量要素管理记录
// Author [yourname](https://github.com/yourname)
func (CETService *CrimeElementTypeService) DeleteCrimeElementType(ctx context.Context, ID string) (err error) {
	err = global.GVA_DB.Delete(&flaud.CrimeElementType{}, "id = ?", ID).Error
	return err
}

// DeleteCrimeElementTypeByIds 批量删除罪量要素管理记录
// Author [yourname](https://github.com/yourname)
func (CETService *CrimeElementTypeService) DeleteCrimeElementTypeByIds(ctx context.Context, IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]flaud.CrimeElementType{}, "id in ?", IDs).Error
	return err
}

// UpdateCrimeElementType 更新罪量要素管理记录
// Author [yourname](https://github.com/yourname)
func (CETService *CrimeElementTypeService) UpdateCrimeElementType(ctx context.Context, CET flaud.CrimeElementType) (err error) {
	err = global.GVA_DB.Model(&flaud.CrimeElementType{}).Where("id = ?", CET.ID).Updates(&CET).Error
	return err
}

// GetCrimeElementType 根据ID获取罪量要素管理记录
// Author [yourname](https://github.com/yourname)
func (CETService *CrimeElementTypeService) GetCrimeElementType(ctx context.Context, ID string) (CET flaud.CrimeElementType, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&CET).Error
	return
}

// GetCrimeElementTypeInfoList 分页获取罪量要素管理记录
// Author [yourname](https://github.com/yourname)
func (CETService *CrimeElementTypeService) GetCrimeElementTypeInfoList(ctx context.Context, info flaudReq.CrimeElementTypeSearch) (list []flaud.CrimeElementType, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&flaud.CrimeElementType{})
	var CETs []flaud.CrimeElementType
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}
	var OrderStr string
	orderMap := make(map[string]bool)
	orderMap["element_name"] = true
	orderMap["element_category"] = true
	orderMap["element_status"] = true
	if orderMap[info.Sort] {
		OrderStr = info.Sort
		if info.Order == "descending" {
			OrderStr = OrderStr + " desc"
		}
		db = db.Order(OrderStr)
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&CETs).Error
	return CETs, total, err
}
func (CETService *CrimeElementTypeService) GetCrimeElementTypePublic(ctx context.Context) {
	// 此方法为获取数据源定义的数据
	// 请自行实现
}
